﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyGUI.Properties;
using MyGUI.WcfFlightRoutingService;

namespace MyGUI
{
    public partial class SearchFlights : Form
    {
        private WcfFlightRoutingService.IService wcfService;

        public SearchFlights()
        {
            wcfService = new WcfFlightRoutingService.ServiceClient();
            InitializeComponent();
            setAirportFrom();
            setAirportTo();
            passengerBox.SelectedIndex = 0;
            deviationComboBox.SelectedIndex = 0;
        }

        public void setAirportFrom()
        {
            List<Airport> AirportNames = wcfService.GetAllAirportNames();
            foreach(Airport i in AirportNames)
            {
                flyFromBox.Items.Add(i.Name);
            }
        }

        public void setAirportTo()
        {
            List<Airport> AirportNames = wcfService.GetAllAirportNames();

            foreach (Airport i in AirportNames)
            {
                flyToBox.Items.Add(i.Name);
            }
        }

        private Airport flyFromComboBox()
        {
            List<Airport> airportArr = wcfService.GetAllAirportNames();
            Airport sourceAirportObj = new Airport();
            for (int i = 0; i < flyFromBox.Items.Count - 1; i++)
            {
                if (flyFromBox.SelectedIndex == i)
                {
                    sourceAirportObj = airportArr.ElementAt(i);
                    //MessageBox.Show(airportObj.IdAirport.ToString());
                }
            }
            return sourceAirportObj;
        }

        private Airport flyToComboBox()
        {
            List<Airport> airportArr = wcfService.GetAllAirportNames();

            Airport destinationAirportObj = new Airport();
            for (int i = 0; i < flyToBox.Items.Count - 1; i++)
            {
                if (flyToBox.SelectedIndex == i)
                {
                    destinationAirportObj = airportArr.ElementAt(i);
                }
            }
            return destinationAirportObj;
        }

        private void allFlightsButton_Click(object sender, EventArgs e)
        {
            AllFlights allFlightsObj = new AllFlights();
            allFlightsObj.Owner = this;
            allFlightsObj.Show();
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            string s = flyFromBox.Text;
            Airport source = flyFromComboBox();
            //MessageBox.Show(source.Name);
            
            Airport target = flyToComboBox();
            //MessageBox.Show(target.Name);

            string departureDate = departureDatePicker.Value.ToString();
            //MessageBox.Show(departureDate);

            int numPassengers = int.Parse(passengerBox.Text);
            //MessageBox.Show(numPassengers.ToString());

            int deviationDays = int.Parse(deviationComboBox.Text);
            #region Handling upperBound, lowerBound and nowBound from DateTime format to string for wClause.
            string upperBound = String.Format("{0:yyyymmdd-HHmm}", departureDatePicker.Value.AddDays(deviationDays));
            string lowerBound = String.Format("{0:yyyymmdd-HHmm}", departureDatePicker.Value.AddDays(-deviationDays));
            string nowBound = String.Format("{0:yyyymmdd-HHmm}", DateTime.Now);
            //Int64 departureDateToInt = Int64.Parse(departureDatePicker.Value.ToString("ddMMyyyy"));
            #endregion

            // Query needs to be revised since msSQL can't handle operations on strings or varchars.
            #region wClause
            string wClause = "ON Flight.idstartairport = Airport.idairport" +
                " WHERE Flight.availableseats - " + numPassengers + " > -6" +
                " AND Flight.timeofdeparture <= " + upperBound +
                " AND Flight.timeofdeparture >= " + lowerBound +
                " AND Flight.timeofdeparture >= " + nowBound;
            MessageBox.Show(wClause);
            #endregion

            //List<Flight> edgeObjList = wcfService.GetCriteriaFlights(wClause, true);
            //List<Flight> edgeObjListAll = wcfService.GetAllFlights(true); 
            //List<Airport> verticeObjList = wcfService.GetAllAirportNames();
            List<Airport> dijkstraList = wcfService.RunDijkstra(source, target);

            #region Test code for printing Dijstra result
            //foreach (Airport a in dijkstraList)
            //{
            //    if (a.AdjFlight == null && a.PreviousVertex == null)
            //    {
            //        Airport nullAirport = new Airport();
            //        Flight nullFlight = new Flight();
            //        nullAirport.Name = "null";
            //        nullFlight.StartAirport = a;
            //        a.PreviousVertex = nullAirport;
            //        //a.AdjFlight = nullFlight;

            //        MessageBox.Show("Vertex: " + a.Name.ToString() + "\n" + "Dist: " + a.Dist.ToString() + "\n" +
            //        "Airport ID: " + a.IdAirport.ToString() + "\n" + "Previous vertex: " + a.PreviousVertex.Name + "\n" +
            //        "Flight departureAirport: " + a.AdjFlight.StartAirport.Name + "\n" +
            //        "Flight destinationAirport " + a.AdjFlight.DestinationAirport.Name);
            //    }
            //    else
            //    {
            //        MessageBox.Show("Vertex: " + a.Name.ToString() + "\n" + "Dist: " + a.Dist.ToString() + "\n" +
            //            "Airport ID: " + a.IdAirport.ToString() + "\n" + "Previous vertex: " + a.PreviousVertex.Name + "\n" +
            //            "Flight id: " + a.AdjFlight.Idflight);
            //    }
            //}
            #endregion

            ShowTravel showTravelObj = new ShowTravel(dijkstraList);
            showTravelObj.Owner = this;
            showTravelObj.Show();
        }

        #region Unused SelectedIndexChanged Methods
        private void flyFromBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void flyToBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        #endregion
    }
}
